<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.*"%>
<%@page import="com.mysql.jdbc.ResultSetMetaData"%>

<%! 
   String strTo(String st) throws Exception{ 
     byte[] bytes=st.getBytes("ISO-8859-1"); 
     String string=new String(bytes,"utf-8");
     return string;
  } 
  ArrayList<String[]> getDBItemNums(Connection conn)throws Exception{
	 ArrayList<String[]> list=new ArrayList<String[]>();
	 Statement st=conn.createStatement();
	 ResultSet rs=st.executeQuery("select * from users");
	 while(rs.next()){ 
       list.add(new String[]{rs.getObject(1).toString(),rs.getObject(2).toString(),rs.getObject(3).toString()});
	 }
	 return list;
  }
  void DBOperate(Connection conn,String clause)throws Exception{ 
	 Statement st=conn.createStatement();
	 int rs=st.executeUpdate(clause);
 }
 %>
 <%

String name=(String)session.getAttribute("name");

   if(request.getParameter("username")==null&&name==null) response.sendRedirect("cache.jsp");
   else if(request.getParameter("username")!=null) session.setAttribute("name", "");

    %>
<%

    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";

    /** 链接数据库参数 **/
  

    //链接数据库并保存到 conn 变量中

    
    Class.forName("com.mysql.jdbc.Driver"); 
    String url = "jdbc:mysql://localhost:3306/db_database10?characterEncoding=UTF-8"; 
    String user = "root"; 
    String password = "123"; 
    Connection conn = null;
    conn = DriverManager.getConnection(url,user,password); 
   
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<link type="text/css" rel="stylesheet" href="css/in.css">
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'sasion.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  <script language="javascript">
//删除选中记录
function selectAll(){
	var table= document.getElementById("user"); //获取table对象
    var rows=table.getElementsByTagName("input");
	for(var i=0;i< table.rows.length;i++){ 
	      rows[i].checked=!rows[i].checked;
	}
/* 	if(tComment.rows.length>1){
	    person_name=tComment.rows[1].cells[0].innerText;
		tComment.deleteRow(1);		//删除表格的第二行，即第一条评论，
	}
	 */
	/* var flag="minus";
	location.href="Transfer.jsp?name="+person_name+"&content=nothing"+"&flag="+flag; */
}
//选中全部记录
function deleteItems(){
	var table = document.getElementById("user"); //获取table对象
	var rows=table.getElementsByTagName("input");
	var name=null;
	for(var i=0;i< rows.length;i++){ 
	   
	   if(rows[i].checked){
	      name=table.rows[i+1].cells[1].innerHTML;
	      table.deleteRow(i+1); 
	      i--;
	   }
	}
	var flag="minus";
location.href="userOperation.jsp?name="+name+"&pwd=null&email=null&flag="+flag; 
}
//显示修改列表
function showChanges(){
	var num=0;
	var name="";
	var password="";
	var email="";	

    var table = document.getElementById("user"); //获取table对象
	var rows=table.getElementsByTagName("input");
/* 	alert(table.rows.length+" "+rows.length);	 */
	for(var m=1;m< table.rows.length;m++){  
	   if(rows[m-1].checked){
	      num=num+1;
	   }
	}
	if(num==0) alert("请选择需要修改的行！");
	else if(num>1) alert("一次只能选中一条数据修改！");
	else if(num==1){                 //如果有选中，即flag==1，显示将要修改的表格
	//获取选中行的数据
	for(var i=1;i<table.rows.length;i++){
       if(rows[i-1].checked) {
       name=table.rows[i].cells[1].innerHTML;
       password=table.rows[i].cells[2].innerHTML;
       email=table.rows[i].cells[3].innerHTML;
       }
    }
    //显示将要显示的修改表格
    var table2= document.getElementById("infor");
    table2.style.display=""; 
    var tr=table2.insertRow(table2.rows.length);
    var td=tr.insertCell(0);
    var button=document.createElement("input");
    button.type="button";
    button.value="确认修改";
    td.align="right";
    var rows2=table2.getElementsByTagName("input");
    rows2[0].value=name;
    rows2[1].value=password;
    rows2[2].value=email;
    button.onclick=function(){changeItems();}; 
    td.appendChild(button); 
    }  
}
//修改选中记录
function changeItems(){ 
      alert("数据修改成功！");
      var table2= document.getElementById("infor");
      var rows2=table2.getElementsByTagName("input");
      var name=rows2[0].value;
      var password=rows2[1].value;
      var email=rows2[2].value;
      var flag="change";
location.href="userOperation.jsp?name="+name+"&pwd="+password+"&email="+email+"&flag="+flag;
}
//显示添加列表
function showAdd(){
    var table= document.getElementById("infor");
    table.style.display=""; 
    var tr=table.insertRow(table.rows.length);
    var td=tr.insertCell(0);
    var button=document.createElement("input");
    button.type="button";
    button.value="添加";
    td.align="right";
    button.onclick=function(){addItems();}; 
    td.appendChild(button);  
}
//添加一条记录
function addItems(){ 
      alert("数据添加成功！");
      var table= document.getElementById("infor");
      var rows=table.getElementsByTagName("input");
      var name=rows[0].value;
      var password=rows[1].value;
      var email=rows[2].value;
      var flag="add";
location.href="userOperation.jsp?name="+name+"&pwd="+password+"&email="+email+"&flag="+flag;
}
function backHome(){ 
location.href="index.jsp";
}

</script>
  <body>
  
   <table width="600" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bordercolorlight="#666666" bordercolordark="#FFFFFF" id="user">
     <tr>
       <td width="15%" align="center" bgcolor="#E5BB93">选中状态</td>
	   <td width="15%" height="27" align="center" bgcolor="#E5BB93">用户名</td>
	   <td width="25%" align="center" bgcolor="#E5BB93">用户密码</td>
	   <td width="45%" align="center" bgcolor="#E5BB93">用户邮箱</td>
     </tr>
     <% 
        ArrayList<String[]> list=getDBItemNums(conn);
        for(int i=0;i<list.size();i++){ 
      %>
      <tr >
       <td align="center" width="15%">
           <input name="check" type="checkbox" value="unchecked" id=<%=i %>>
       </td>
	   <td width="15%" ><%=list.get(i)[0] %></td>
	   <td width="25%" ><%=list.get(i)[1]  %></td>
	   <td width="45%" ><%=list.get(i)[2]  %></td>
      </tr>
     <%} %>
   </table>
   <div align="center">
   <br>
   <input name="Button" type="button" class="btn_grey" value="全部选中" onClick="selectAll()"></td>
   &nbsp;
    <input name="Button" type="button" class="btn_grey" value="添加用户" onClick="showAdd()"> 
   &nbsp;
   <input name="Button" type="button" class="btn_grey" value="修改选中" onClick="showChanges()">
   &nbsp;
   <input name="Button" type="button" class="btn_grey" value="删除选中" onClick="deleteItems()">      
   &nbsp;
   <input name="Button" type="button" class="btn_grey" value="返回首页" onClick="backHome()">     
   </div>
   <table  style="display:none" id="infor" width="600" height="122" border="0" align="center" cellpadding="0" cellspacing="0" >
       <tr>
         <td width="119" height="14">&nbsp;</td>
         <td width="481">&nbsp;</td>
       </tr>
       <tr>
         <td height="27" align="center">用户名：</td>
         <td>
           <input name="person" type="text" id="person" size="25">
         </td>
       </tr>
       <tr>
         <td align="center">用户密码：</td>
         <td>
            <input name="person" type="text" id="person" size="25">
         </td>
       </tr>
       <tr>
         <td align="center">邮      箱：</td>
         <td>
            <input name="person" type="text" id="person" size="25">
         </td>
       </tr>
  <!--      <tr >
          <td>
         <input name="person" type="button" id="tijiao" size="15" value="提交" onClick="">
          </td>      
       </tr> -->
    </table>
 </body>
</html>
